export default {
	namespaced: true,
	
	state: () => ({
		cartList: JSON.parse(uni.getStorageSync('cart') || '[]')
	}),
	
	mutations: {
		addToCart(state, goods) {
			const findResult = state.cartList.find(x => x.goodsId === goods.goodsId && x.createTime === goods.createTime)
			if(!findResult) {
				state.cartList.push(goods)
			}else {
				findResult.goodsCount++
				findResult.goodsImg = goods.goodsImg
				findResult.goodsPrice = goods.goodsPrice
				findResult.goodsOptions = goods.goodsOptions
			}
			this.commit('module_cart/saveToStorage')
		},
		saveToStorage(state) {
			uni.setStorageSync('cart', JSON.stringify(state.cartList))
		},
		updateGoodsState(state, goods) {
			console.log(goods);
			const findResult = state.cartList.find(x => x.goodsId === goods.goodsId && x.createTime === goods.createTime)
			console.log(findResult);
			if(findResult) {
				findResult.goodsState = goods.goodsState
				if(findResult.userId) {
					uni.$http.put(`/user/carts`, findResult)
				}
				this.commit('module_cart/saveToStorage')
			}
		},
		updateGoodsCount(state, goods) {
			const findResult = state.cartList.find(x => x.goodsId === goods.goodsId && x.createTime === goods.createTime)
			if(findResult) {
				findResult.goodsCount = goods.goodsCount
				
				this.commit('module_cart/saveToStorage')
			}
		},
		removeGoodsById(state, goods) {
			state.cartList = state.cartList.filter(x => { 
				return !(x.goodsId === goods.goodsId && x.createTime === goods.createTime )
			})
			console.log('after', state.cartList);
			if(goods.userId) {
				uni.$http.delete('/user/carts', goods)
			}
			this.commit('module_cart/saveToStorage')
		},
		updateAllGoodsState(state, newState) {
			state.cartList.forEach(x => x.goodsState = newState)
			console.log(state.cartList.every(item => item.userId));
			let { id } = JSON.parse(uni.getStorageSync('userinfo') || '[]')
			console.log(id);
			console.log(state.cartList);
			if(state.cartList.every(item => item.userId) && id) {
				uni.$http.put(`/user/carts/all`, state.cartList)
			}
			this.commit('module_cart/saveToStorage')
		},
		updateCart(state, cart) {
			state.cartList = cart
			this.commit('module_cart/saveToStorage')
		}
	},
	
	getters: {
		total(state) {
			console.log(state.cartList.reduce((total, item) => total += item.goodsCount, 0));
			return state.cartList.reduce((total, item) => total += item.goodsCount, 0)
		},
		checkedCount(state) {
			return state.cartList.filter(x => x.goodsState).reduce((total, item) => total += item.goodsCount, 0)
		},
		checkedGoodsAmount(state) {
			return state.cartList.filter(x => x.goodsState)
			.reduce((total, item) => total += item.goodsCount * item.goodsPrice, 0)
			.toFixed(2)
		},
		checkedGoods(state) {
			return state.cartList.filter(x => x.goodsState)
		}
	}
}

// export default {
// 	namespaced: true,
// 	state: () => ({
// 		cartListList: JSON.parse(uni.getStorageSync('cart') || '[]')
// 	}),
// 	mutations: {
// 		addToCart(state, goods) {
// 			console.log('target', goods);
// 			const findResult = state.cartList.find(x => x.goodsId === goods.goodsId)
// 			console.log(findResult);
// 			if(!findResult) {
// 				state.cartList.push(goods)
// 			}else {
// 				findResult.goodsCount++
// 				findResult.goodsImg = goods.goodsImg
// 				findResult.goodsPrice = goods.goodsPrice
// 				findResult.goodsOptions = goods.goodsOptions
// 			}
// 			this.commit('module_cart/saveToStorage')
// 		},
// 		saveToStorage(state) {
// 			uni.setStorageSync('cart', JSON.stringify(state.cartList))
// 		},
// 		updateGoodsState(state, goods) {
// 			console.log(goods);
// 			const findResult = state.cartList.find(item => item.goodsId = goods.goodsId)
// 			console.log('findResult',findResult);
// 			if(findResult) {
// 				findResult.goodsState = goods.goodsState
// 				this.commit('module_cart/saveToStorage')
// 			}
// 		},
// 		updateGoodsCount(state, goods) {
// 			const findResult = state.cartList.find(item => item.goodsId === goods.goodsId)
// 			findResult.goodsCount = goods.goodsCount
// 			this.commit('module_cart/saveToStorage')
// 		},
// 		updateAllGoodsState(state, newState) {
// 			console.log('newState', newState);
// 			state.cartList.forEach(item => {
// 				item.goodsState = newState
// 			})
// 			console.log('allChange', item.goodsState);
// 			this.commit('module_cart/saveToStorage')
// 		},
// 		removeGoodsById(state, goodsId) {
// 			state.cart = state.cartList.filter(item => item.goodsId != goodsId)
// 			this.commit('module_cart/saveToStorage')
// 		}
// 	},
// 	getters: {
// 		total(state) {
// 			console.log('total');
// 			return state.cartList.reduce((total, item) => total += item.goodsCount, 0)
// 		},
// 		checkedCount(state) {
// 			// console.log('checkedCount');
// 			return state.cartList.filter(item => item.goodsState).reduce((count, item) => count += item.goodsCount, 0)
// 		},
// 		checkedGoodsAmount(state) {
// 			// console.log('amount');
// 			return state.cartList.filter(item => item.goodsState)
// 			.reduce((total, item) => total += item.goodsCount * item.goodsPrice, 0)
// 			.toFixed(2)
// 		}
// 	}
// }